package com.xjprojectc.base.job;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import sun.management.resources.agent;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.tech.framework.service.impl.BaseServiceImpl;
import com.xjprojectc.base.dao.DataDaoImpl;
import com.xjprojectc.business.pojo.T_agentaccountinfo;
import com.xjprojectc.business.pojo.T_callpaymentrecord;
import com.xjprojectc.business.pojo.T_prepaidcard;
import com.xjprojectc.uitl.DateUtil;
import com.xjprojectc.uitl.JsonUtil;
import com.xjprojectc.webservice.Call;


/**
 * 更新服务端数据
 * @author Administrator
 */
@SuppressWarnings("unchecked")
public class SystemJob extends BaseServiceImpl {
	public void readServerData(){
		System.out.println("服务器数据跟新");
		String result = Call.getCall().exRevisionData("");
		if(result.length() > 2){
			JSONObject obj = JSONObject.fromObject(result);
			Set set = obj.keySet();
			Iterator<String> iterator = set.iterator();
			int entityId = 0;
			while (iterator.hasNext()) {
				String entity = iterator.next();
				if(entity.equals("agentaccountinfo")){
					JSONArray array = JSONArray.fromObject(obj.get(entity));
					if(array.size()>0){
						Map<Integer,String> updatas = new HashMap<Integer, String>();
						T_agentaccountinfo agent = null;
						T_agentaccountinfo info = new T_agentaccountinfo();
						List<T_agentaccountinfo> infoList = null;
						for (int i = 0, len = array.size(); i < len; i++) {
							JSONObject data = array.getJSONObject(i);
							agent = (T_agentaccountinfo) JsonUtil.getObject4JsonString(data.toString(), T_agentaccountinfo.class);
							if(agent.getUsername() != null){
								entityId = agent.getId();
								info.setUsername(agent.getUsername());
								infoList = this.find(T_agentaccountinfo.class.getName()+".select", info);
								if(infoList != null &&  infoList.size()>0){
									agent.setId(infoList.get(0).getId());
									agent.setAccountmoney(null);
									agent.setField2(null);
									agent.setField3(null);
									if(agent.getCtsmsserver() == null) agent.setCtsmsserver("");
									if(agent.getCmsmsserver() == null) agent.setCmsmsserver("");
									if(agent.getCusmsserver() == null) agent.setCusmsserver("");
									this.update(agent);
								}else{
									this.insert(agent);
								}
								updatas.put(entityId, "agentaccountinfo");
							}	
						}
						if(updatas.size() >0){
							obj = new JSONObject();
							obj.putAll(updatas);
							Call.getCall().exRevisionDataUpdateState(obj.toString());
						}
					}	
				}else if(entity.equals("prepaidcard")){
					JSONArray array = JSONArray.fromObject(obj.get(entity));
					if(array.size()>0){
						Map<Integer,String> updatas = new HashMap<Integer, String>();
						T_prepaidcard card = null;
						T_prepaidcard selCard = new T_prepaidcard();
						List<T_prepaidcard> cardList = null;
						for (int i = 0, len = array.size(); i < len; i++) {
							JSONObject data = array.getJSONObject(i);
							card = (T_prepaidcard) JsonUtil.getObject4JsonString(data.toString(), T_prepaidcard.class);
							if(card.getCardnumber() != null && card.getCardpwd() != null){
								entityId = card.getId();
								selCard.setCardnumber(card.getCardnumber());
								selCard.setCardpwd(card.getCardpwd());
								cardList = this.find(T_prepaidcard.class.getName()+".select", selCard);
								if(cardList != null &&  cardList.size()>0){
									card.setId(cardList.get(0).getId());
									this.update(card);
								}else{
									this.insert(card);
								}
								updatas.put(entityId, "prepaidcard");
							}	
						}
						if(updatas.size() >0){
							obj = new JSONObject();
							obj.putAll(updatas);
							Call.getCall().exRevisionDataUpdateState(obj.toString());
						}
					}	
				}
			}
		}	
	}
	
	public void refrishRechargeState(){
		System.out.println("充值状态跟新");
		String time = DateUtil.getNearTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "h", -3);
		T_callpaymentrecord call = new T_callpaymentrecord();
		call.setStart_time(time);
		call.setStatus("0");
		List<T_callpaymentrecord> list = this.find(T_callpaymentrecord.class.getName()+".select", call);
		if(list != null && list.size()>0){
			StringBuffer sb = new StringBuffer();
			for(T_callpaymentrecord c : list){
				sb.append(c.getSerialnumber());
				sb.append(",");
;			}
			String result = Call.getCall().exRechargeState(sb.toString());
			JSONObject obj  = JSONObject.fromObject(result);
			if(obj.get("success").toString().equals("true") && obj.get("data").toString().length()>5){
				JSONArray array = JSONArray.fromObject(obj.get("data").toString());
				if(array.size()>0){
					List<String> serialnumbers = new ArrayList<String>();
					for (int i = 0, len = array.size(); i < len; i++) {
						JSONObject data = array.getJSONObject(i);
						serialnumbers.add(data.get("recharge_id").toString());
					}
					if(serialnumbers.size() > 0){
						update(T_callpaymentrecord.class.getName()+".updateRecordState", serialnumbers);
					}
				}	
			}
		}
	}
	
	public void updateManagerBalance(){
		System.out.println("管理员余额跟新");
		T_agentaccountinfo agent = new T_agentaccountinfo();
		agent.setAgentlevel(1);
		List<T_agentaccountinfo> agentList = find(T_agentaccountinfo.class.getName()+".select", agent);
		T_callpaymentrecord call = null;
		for(T_agentaccountinfo age : agentList){
			call = new T_callpaymentrecord();
			call.setId(Integer.parseInt(age.getField3()));
			call.setAgentname(age.getUsername());
			List<T_callpaymentrecord> list = find(T_callpaymentrecord.class.getName()+".selectManagerBalance", call);
			if(list != null && list.size()>0 && list.get(0).getCostprice() != null && list.get(0).getCostprice() >0){
				call =  list.get(0);
				DataDaoImpl.updateAgentBalance("reduce", call.getCostprice(), age.getUsername(), call.getId().toString());
			}
		}
	}
}
